﻿using DatabaseInvoke;
using DataModel;
using System;
using System.Text;

namespace TSC
{
    public class TempratureRecord
    {
        private SqlManipulation sqlMainpul;
        //private ConfigUtility configUtility;
        //private string Connectionstring;

        //public ConcurrentDictionary<int, TempratureModel> cableTemps = new ConcurrentDictionary<int, TempratureModel>();
        //public ConcurrentQueue<TempratureModel> tempratureModels = new ConcurrentQueue<TempratureModel>();

        public TempratureRecord()
        {
            //configUtility = _configUtility;
            //Connectionstring = _connString;
            sqlMainpul = new SqlManipulation(GlobalVar.ConnectionString, SqlType.PostgresQL);
        }

        //internal void BulkUpdateTemprature(List<TempratureModel> lstTemp)
        //{
        //    //形成SQL语句，调用事务，写入数据库。
        //    if (!lstTemp.Any())
        //        return;
        //    List<string> lstSql = new List<string>();

        //    foreach (var item in lstTemp)
        //    {
        //        StringBuilder sb = new StringBuilder();
        //        sb.Clear();
        //        sb.Append(" UPDATE temperature_sensor SET ");
        //        sb.Append(" avg = " + item.Average + ",");
        //        sb.Append(" max = " + item.Max + ",");

        //        if (item.IsEnable)
        //        {
        //            if (item.IsValid)
        //            {
        //                sb.Append(" isvalid = " + item.IsEnable + ",");
        //                for (int i = 0; i < item.Values.Count; i++)
        //                {
        //                    sb.Append(" Value" + (i + 1) + " = " + item.Values[i] + ",");
        //                }
        //            }
        //            else
        //            {
        //                continue;
        //            }
        //        }
        //        else
        //        {
        //            sb.Append(" isvalid = " + item.IsEnable + ",");
        //        }
        //        sb.Append(" created_time = '" + item.UpdatedTime.ToString("yyyy-MM-dd HH:mm:ss") + "'");
        //        sb.Append(" WHERE id = " + item.CableId);

        //        lstSql.Add(sb.ToString());
        //    }

        //    //lock (sqlMainpul)
        //    //{
        //        int iRet = sqlMainpul.ExecuteTransaction(lstSql);

        //        if (iRet < 0)
        //        {
        //            Log.Error("感温电缆写入数据库错误！", true);
        //        }
        //    //}
        //}

        //internal void SetAllCableDisabled()
        //{
        //    StringBuilder sb = new StringBuilder();
        //    sb.Clear();
        //    sb.Append("UPDATE temperature_sensor SET isvalid = FALSE");
        //    sqlMainpul.ExcuteNonQueryNew(sb.ToString());
        //}

        internal void AddAlarmTo3D(int v1, string v2, string v3)
        {
            StringBuilder sb = new StringBuilder();
            sb.Clear();
            sb.Append("Insert into warn_log(type,created_time,name,remark)values(" + v1 + ",'" + DateTime.Now + "','" + v2 + "','" + v3 + "')");
            sqlMainpul.ExcuteNonQueryNew(sb.ToString());
        }

        //internal void Init()
        //{
        //    //在这里需要连接数据库。
        //    sqlMainpul.Init();
        //}

        //internal async void RunAsync(CancellationToken token)
        //{
        //    while (!token.IsCancellationRequested)
        //    {
        //        if (tempratureModels.TryDequeue(out TempratureModel model))
        //        {
        //            UpdateTemprature(model);
        //        }
        //        else
        //        {
        //            await Task.Delay(2000);
        //        }
        //    }
        //}

        //private void UpdateTemprature(TempratureModel model)
        //{
        //    throw new NotImplementedException();
        //}

        //internal void UpdateTemprature(int stationId, double average, double max)
        //{
        //    try
        //    {
        //        //在这里调用存储过程写入。
        //        string sql = "SELECT COUNT(*) FROM temperature_sensor WHERE id = '" + stationId + "';";
        //        int num = sqlMainpul.GetTaskId(sql);
        //        if (num <= 0)
        //        {
        //            sql = "INSERT INTO temperature_sensor(id, avg, max, created_time) VALUES('" + stationId + "', '" + average + "', '" + max + "', '" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "');";

        //            sqlMainpul.ExcuteNonQueryNew(sql);
        //        }
        //        else
        //        {
        //            sql = "UPDATE temperature_sensor SET avg = '" + average + "', max = '" + max + "', created_time = '" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "' WHERE id = '" + stationId + "'";
        //            sqlMainpul.ExcuteNonQueryNew(sql);
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        ;
        //    }
        //}
    }
}